import React from 'react';

const coverDate = [
  {
    type: 'SINGLE_VALUE',
    code: 'load_date',
    name: '数据日期',
    id: null,
    range: 'GLOBAL',
    display: null,
    operate: 'EQ',
    filterComponent: {
      compType: 'DATE',
      dateType: 'DATEPICKER',
      code: 'YYYY',
      picker: 'year',
    },
    selectableValues: [],
    valueDataType: 'STRING',
    value: '2023',
  },
  {
    type: 'SINGLE_VALUE',
    code: 'load_date1',
    name: '数据日期',
    id: null,
    range: 'GLOBAL',
    display: null,
    operate: 'EQ',
    filterComponent: {
      compType: 'DATE',
      dateType: 'DATEPICKER',
      code: 'YYYY-Q',
      picker: 'year',
    },
    selectableValues: [],
    valueDataType: 'STRING',
    value: '2023-6',
  },
  {
    type: 'RANGE_VALUE',
    code: 'date1',
    name: '数据日期',
    id: null,
    range: 'GLOBAL',
    display: null,
    operate: 'BETWEENCMM',
    filterComponent: {
      compType: 'DATE',
      dateType: 'DATEPICKER',
      code: 'YYYY-MM',
      picker: 'year-MM',
    },
    selectableValues: [],
    valueDataType: 'STRING',
    value: {
      startValue: '',
      endValue: '',
    },
    startValue: '2008-01',
    endValue: '2008-03',
  },
  {
    type: 'RANGE_VALUE',
    code: 'date2',
    name: '数据日期',
    id: null,
    range: 'GLOBAL',
    display: null,
    operate: 'BETWEENCMM',
    filterComponent: {
      compType: 'DATE',
      dateType: 'DATEPICKER',
      code: 'YYYY-MM-DD',
      picker: 'YYY-MM-dd',
    },
    selectableValues: [],
    valueDataType: 'STRING',
    value: {
      startValue: '',
      endValue: '',
    },
    startValue: '2021-01-11',
    endValue: '2021-03-12',
  },
];

const origin = [
  {
    type: 'RANGE_VALUE',
    code: 'date2',
    name: '数据日期',
    id: null,
    range: 'GLOBAL',
    display: null,
    operate: 'BETWEENCMM',
    filterComponent: {
      compType: 'DATE',
      dateType: 'DATEPICKER',
      code: 'YYYY-MM-DD',
      picker: 'YYY-MM-dd',
    },
    selectableValues: [],
    valueDataType: 'STRING',
    value: {
      startValue: '',
      endValue: '',
    },
    startValue: '2008-01-11',
    endValue: '2008-03-12',
  },
  {
    type: 'RANGE_VALUE',
    code: 'load_date',
    name: '数据日期',
    id: null,
    range: 'GLOBAL',
    display: null,
    operate: 'BETWEENCMM',
    filterComponent: {
      compType: 'DATE',
      dateType: 'DATEPICKER',
      code: 'YYYY',
      picker: 'year',
    },
    selectableValues: [],
    valueDataType: 'STRING',
    value: {
      startValue: '',
      endValue: '',
    },
    startValue: '',
    endValue: '',
  },
  {
    type: 'SINGLE_VALUE',
    code: 'date1',
    name: '数据日期',
    id: null,
    range: 'GLOBAL',
    display: null,
    operate: 'EQ',
    filterComponent: {
      compType: 'DATE',
      dateType: 'DATEPICKER',
      code: 'YYYY-MM',
      picker: 'year',
    },
    selectableValues: [],
    valueDataType: 'STRING',
    value: '2021-03',
  },
  {
    type: 'SINGLE_VALUE',
    code: 'load_date1',
    name: '数据日期',
    id: null,
    range: 'GLOBAL',
    display: null,
    operate: 'EQ',
    filterComponent: {
      compType: 'DATE',
      dateType: 'DATEPICKER',
      code: 'YYYY-Q',
      picker: 'year',
    },
    selectableValues: [],
    valueDataType: 'STRING',
    value: '2023-1',
  },
];

const covertParam = [
  {
    code: 'project_type',
    display: null,
    filterComponent: { compType: 'SELECT' },
    id: null,
    name: '项目类型',
    operate: 'EQ',
    range: 'GLOBAL',
    selectableValues: null,
    type: 'SINGLE_VALUE',
    value: 500,
    valueDataType: 'STRING',
  },
  {
    code: 'org_type',
    display: null,
    filterComponent: { compType: 'TREE', level: '0', treeType: 'ORG_TREE' },
    id: null,
    name: '机构类型',
    operate: 'EQ',
    range: 'GLOBAL',
    selectableValues: null,
    type: 'MULTI_VALUE',
    value: ['123', '456'],
    valueDataType: 'STRING',
  },
  {
    code: 'project_type1',
    display: null,
    filterComponent: { compType: 'SELECT' },
    id: null,
    name: '项目类型',
    operate: 'EQ',
    range: 'GLOBAL',
    selectableValues: null,
    type: 'SINGLE_VALUE',
    value: ['1001-200'],
    valueDataType: 'STRING',
  },
];

let originParam = [
  {
    code: 'load_date',
    display: null,
    filterComponent: {
      compType: 'DATE',
      dateType: 'DATEPICKER',
      code: 'YYYY-Q',
      picker: 'quarter',
    },
    id: null,
    name: '数据日期',
    operate: 'EQ',
    range: 'GLOBAL',
    selectableValues: null,
    type: 'MULTI_VALUE',
    startValue: '2021-01',
    endValue: '2021-04',
    value: '2020-4',
    valueDataType: 'STRING',
  },
  {
    code: 'org_type',
    display: null,
    filterComponent: { compType: 'TREE', level: '0', treeType: 'ORG_TREE' },
    id: null,
    name: '机构类型',
    operate: 'EQ',
    range: 'GLOBAL',
    selectableValues: null,
    type: 'SINGLE_VALUE',
    value: null,
    valueDataType: 'STRING',
  },
  {
    code: 'project_type',
    display: null,
    filterComponent: { compType: 'SELECT' },
    id: null,
    name: '项目类型',
    operate: 'EQ',
    range: 'GLOBAL',
    selectableValues: null,
    type: 'SINGLE_VALUE',
    value: null,
    valueDataType: 'STRING',
  },
  {
    code: 'project_type1',
    display: null,
    filterComponent: { compType: 'SELECT' },
    id: null,
    name: '项目类型',
    operate: 'EQ',
    range: 'GLOBAL',
    selectableValues: null,
    type: 'MULTI_VALUE',
    value: '100',
    valueDataType: 'STRING',
  },
  {
    code: 'datetype',
    display: null,
    filterComponent: {
      compType: 'DATE',
      dateType: 'DATEPICKER',
      code: 'YYYY-MM',
      picker: 'quarter',
    },
    id: null,
    name: '日期类型',
    operate: 'EQ',
    range: 'GLOBAL',
    selectableValues: null,
    type: 'MULTI_VALUE',
    value: '2020-03',
    startValue: '2008-01',
    endValue: '2008-02',
    valueDataType: 'STRING',
  },
];

const converTree = [
  {
    code: 'org_no',
    display: null,
    filterComponent: { compType: 'TREE', level: '1', treeType: 'ORG_TREE' },
    compType: 'TREE',
    level: '1',
    treeType: 'ORG_TREE',
    id: null,
    name: '机构',
    operate: 'EQ',
    range: 'GLOBAL',
    selectableValues: null,
    type: 'MULTI_VALUE',
    value: '1001',
    valueDataType: 'STRING',
  },
  {
    code: 'qydm',
    display: null,
    filterComponent: { compType: 'TREE', level: 'CITY', treeType: 'AREA_TREE' },
    id: null,
    name: '地区',
    operate: 'EQ',
    range: 'GLOBAL',
    selectableValues: null,
    type: 'SINGLE_VALUE',
    value: 1000,
    valueDataType: 'STRING',
  },
];

const originTree = [
  {
    code: 'qydm',
    display: null,
    filterComponent: { compType: 'TREE', level: 'CITY', treeType: 'AREA_TREE' },
    id: null,
    name: '地区',
    operate: 'EQ',
    range: 'GLOBAL',
    selectableValues: null,
    type: 'SINGLE_VALUE',
    value: null,
    valueDataType: 'STRING',
  },
  {
    code: 'org_no',
    display: null,
    filterComponent: { compType: 'TREE', level: '1', treeType: 'ORG_TREE' },
    compType: 'TREE',
    level: '1',
    treeType: 'ORG_TREE',
    id: null,
    name: '机构',
    operate: 'EQ',
    range: 'GLOBAL',
    selectableValues: null,
    type: 'SINGLE_VALUE',
    value: '100001',
    valueDataType: 'STRING',
  },
];

const test1: never[] = [];
const test2 = [
  {
    code: 'datetype',
    display: null,
    filterComponent: {
      compType: 'DATE',
      dateType: 'DATEPICKER',
      code: 'YYYY-MM',
      picker: 'quarter',
    },
    id: null,
    name: '日期类型',
    operate: 'EQ',
    range: 'GLOBAL',
    selectableValues: null,
    type: 'MULTI_VALUE',
    value: '2020-03',
    startValue: '2008-01',
    endValue: '2008-02',
    valueDataType: 'STRING',
  },
];

const tansfromParams = (item: any, prve: any) => {
  const commonArr = item.filter((obj: any) =>
    prve.some((obj1: any) => {
      //handleCommon(obj,obj1)
      if (obj.filterComponent.compType === 'DATE') {
        return (
          obj.code === obj1.code &&
          obj.filterComponent.compType === obj1.filterComponent.compType &&
          obj.filterComponent.code === obj1.filterComponent.code
        );
      } else if (obj.filterComponent.compType === 'TREE') {
        return (
          obj.code === obj1.code &&
          obj.filterComponent.compType === obj1.filterComponent.compType &&
          obj.filterComponent.treeType === obj1.filterComponent.treeType
        );
      } else {
        return (
          obj.code === obj1.code &&
          obj.filterComponent.compType === obj1.filterComponent.compType
        );
      }
    }),
  );
  console.log(commonArr);
  if (commonArr.length > 0) {
    for (let i = 0; i < prve.length; i++) {
      const findItem = commonArr.find((value: any) => {
        if (prve[i].filterComponent.compType === 'DATE') {
          if (
            value.code === prve[i].code &&
            value.filterComponent.compType ===
              prve[i].filterComponent.compType &&
            value.filterComponent.code === prve[i].filterComponent.code
          ) {
            return value;
          }
        } else if (prve[i].filterComponent.compType === 'TREE') {
          if (
            value.code === prve[i].code &&
            value.filterComponent.compType ===
              prve[i].filterComponent.compType &&
            value.filterComponent.treeType === prve[i].filterComponent.treeType
          ) {
            return value;
          }
        } else {
          if (
            value.code === prve[i].code &&
            value.filterComponent.compType === prve[i].filterComponent.compType
          ) {
            return value;
          }
        }
      });
      console.log(findItem);
      if (findItem !== undefined) {
        prve[i] = handleCovertValue(findItem, prve[i]);
      }
    }
    return prve;
  } else {
    return prve;
  }
};

const handleCovertValue = (
  convertValue: any,
  originVal: {
    filterComponent: { compType: string };
    value: any;
    type: any;
    endValue: any;
    startValue: any;
  },
) => {
  switch (originVal.type) {
    case 'SINGLE_VALUE':
      if (convertValue.type === 'SINGLE_VALUE') {
        originVal.value = convertValue.value;
      } else if (convertValue.type === 'RANGE_VALUE') {
        if (convertValue.filterComponent.compType === 'SELECT') {
          originVal.value = convertValue.value;
        } else {
          originVal.value = convertValue.endValue;
        }
      } else if (convertValue.type === 'MULTI_VALUE') {
        originVal.value = convertValue.value;
      }
      break;
    case 'RANGE_VALUE':
      if (convertValue.type === 'SINGLE_VALUE') {
        if (convertValue.filterComponent.compType === 'SELECT') {
          originVal.value = convertValue.value;
        } else {
          originVal.endValue = convertValue.value;
        }
      } else if (convertValue.type === 'RANGE_VALUE') {
        if (convertValue.filterComponent.compType === 'SELECT') {
          originVal.value = convertValue.value;
        } else {
          originVal.startValue = convertValue.startValue;
          originVal.endValue = convertValue.endValue;
        }
      } else if (convertValue.type === 'MULTI_VALUE') {
        if (convertValue.filterComponent.compType === 'SELECT') {
          originVal.value = convertValue.value;
        } else {
          originVal.endValue = convertValue.value;
        }
      }
      break;
    case 'MULTI_VALUE':
      if (convertValue.type === 'SINGLE_VALUE') {
        originVal.value = convertValue.value;
      } else if (convertValue.type === 'RANGE_VALUE') {
        if (convertValue.filterComponent.compType === 'SELECT') {
          originVal.value = convertValue.value;
        } else {
          originVal.value = convertValue.endValue;
        }
      } else if (convertValue.type === 'MULTI_VALUE') {
        originVal.value = convertValue.value;
      }
      break;
    default:
      return originVal;
  }
  return originVal;
};

// const res = tansfromParams(covertParam,originParam)
// console.log(res)

// const res2 = tansfromParams(coverDate,origin)
// console.log(res2)

// const res3 = tansfromParams(test2,test1)

// console.log(res3)

// const res4 = tansfromParams(converTree,originTree)
// console.log(res4)

const test = [
  {
    type: 'SINGLE_VALUE',
    code: 'load_date',
    name: '数据日期',
    id: null,
    range: 'GLOBAL',
    display: null,
    operate: 'EQ',
    filterComponent: {
      compType: 'DATE',
      dateType: 'DATEPICKER',
      code: 'YYYY-MM-DD',
      picker: 'date',
    },
    selectableValues: [],
    valueDataType: 'STRING',
    value: '2021-04-04',
  },
];

const test11 = [
  {
    type: 'RANGE_VALUE',
    code: 'load_date',
    name: '数据日期',
    id: null,
    range: 'GLOBAL',
    display: null,
    operate: 'BETWEENCMM',
    filterComponent: {
      compType: 'DATE',
      dateType: 'DATEPICKER',
      code: 'YYYY-MM-DD',
      picker: 'date',
    },
    selectableValues: [],
    valueDataType: 'STRING',
    value: {
      startValue: '',
      endValue: '',
    },
    startValue: '',
    endValue: '',
  },
];

const res4 = tansfromParams(test, test11);
console.log(res4);
